package com.module.overview.ui.editCardList

import androidx.lifecycle.MutableLiveData
import com.common.app.data.bean.KeyPre
import com.common.app.data.bean.overView.CardSettingListModel
import com.common.app.data.bean.overView.CardSettingModel
import com.common.base.app.viewmodel.BaseViewModel
import com.common.base.utils.AppUtils
import com.common.base.utils.SpUtilsManagement
import com.module.ble.utils.AllCardTypeConfig
import com.module.overview.R

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2025/06/15
 *
 * 描述：卡片编辑ViewModel，处理卡片数据的加载和保存
 *
 * 修订历史：
 *
 */
class EditCardViewModel : BaseViewModel() {

    // 卡片设置数据LiveData
    val cardSettings = MutableLiveData<List<CardSettingModel>>()

    init {
        var cardSettingListModel: CardSettingListModel? = null
        try {
            cardSettingListModel = SpUtilsManagement.getParcelable(KeyPre.KEY_OV_CARD_CONFIG, CardSettingListModel::class.java)
        } catch (e: Exception) {
            // 数据损坏，清理并重新初始化
            SpUtilsManagement.removeFromKey(KeyPre.KEY_OV_CARD_CONFIG)
        }
        if(cardSettingListModel==null){
            //首次构造概览UI配置
            val mData = mutableListOf<CardSettingModel>()
            mData.add(CardSettingModel(name = AppUtils.getString(R.string.common_sleep_hint), sortOrder = 1, itemType = AllCardTypeConfig.TYPE_CARD_SLEEP))
            mData.add(CardSettingModel(name = AppUtils.getString(R.string.common_activity_hint), sortOrder = 2,itemType = AllCardTypeConfig.TYPE_CARD_ACTIVITY))
            mData.add(CardSettingModel(name = AppUtils.getString(R.string.common_pressure_hint), sortOrder = 3,itemType = AllCardTypeConfig.TYPE_CARD_PRESSURE))
            mData.add(CardSettingModel(name = AppUtils.getString(R.string.common_signs_hint), sortOrder = 4,itemType = AllCardTypeConfig.TYPE_CARD_SIGNS))
            mData.add(CardSettingModel(name = AppUtils.getString(R.string.common_exercise_hint), sortOrder = 5,itemType = AllCardTypeConfig.TYPE_CARD_EXERCISE))
//            mData.add(CardSettingModel(name = AppUtils.getString(R.string.common_period_mg_hint), sortOrder = 6,itemType = AllCardTypeConfig.TYPE_CARD_PERIOD))
            cardSettingListModel = CardSettingListModel(mData)
            SpUtilsManagement.putParcelable(KeyPre.KEY_OV_CARD_CONFIG, cardSettingListModel)
        }
    }

    // 更新卡片状态
    fun updateCardStatus(card: CardSettingModel) {
        // 在实际应用中，这里应该调用Repository保存数据到数据库
        // 例如：cardRepository.updateCard(card)
        
        // 模拟保存成功
        // 可以发送事件通知UI更新
        // sendEvent(Event.ShowToast("卡片状态已更新"))
    }
    
    // 加载卡片设置数据
    fun loadCardSettings() {
        try {
            SpUtilsManagement.getParcelable(KeyPre.KEY_OV_CARD_CONFIG, CardSettingListModel::class.java)?.let { cardSettingListModelIt ->
                cardSettingListModelIt.mData?.let { dataList ->
                    val validData = dataList.filterNotNull()
                    if (validData.isNotEmpty()) {
                        val mutableData = validData.toMutableList()
                        mutableData.sortBy { it.sortOrder }
                        cardSettings.value = mutableData
                    } else {
                        cardSettings.value = mutableListOf()
                    }
                } ?: run {
                    cardSettings.value = mutableListOf()
                }
            }
        } catch (e: Exception) {
            // 处理异常，重新初始化
            SpUtilsManagement.removeFromKey(KeyPre.KEY_OV_CARD_CONFIG)
            cardSettings.value = mutableListOf()
        }
    }
    
    // 保存卡片设置数据
    fun saveCardSettings(cards: List<CardSettingModel>) {
        // 在实际应用中，这里应该调用Repository保存数据到数据库
        // 例如：cardRepository.saveCards(cards)
        
        // 模拟保存成功
        // 可以发送事件通知UI更新
        // sendEvent(Event.ShowToast("卡片设置已保存"))
    }
}